/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.DAO;

import DBConnect.DBConnect;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Hoang Quan
 */
public class ProductDAO implements IProduct{

    @Override
    public ArrayList<Product> getAll() {
      //  throw new UnsupportedOperationException("Not supported yet.");
        ArrayList<Product> list = null;
        
        //ket noi db lay 1 danh sach Product
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        if(DBConnect.open()){
            try {
                ps= DBConnect.cnn.prepareStatement("select * from product");
                rs = ps.executeQuery();
                list = new ArrayList<Product>();
                
                while (rs.next()) {                   
                 list.add(new Product(rs.getString("ID"), rs.getString("name"),rs.getString("type"),rs.getString("provider"),rs.getInt("price"),rs.getString("image"),rs.getString("detail"),rs.getInt("quantity"),rs.getString("state")));
                    
                }
            } catch (SQLException ex) {
                Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
            
                DBConnect.close(ps, rs);
            }
            
        
        }else{
        
            //phai mo ra 1 form cho nhap user nam passs, url , driver
            JOptionPane.showMessageDialog(null, "Sai cau hinh ket noi den server");
        }
        
        
        return  list;
    }
    
}
